package com.ivan.stu.notetool.View;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Rect;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.ivan.stu.notetool.R;


/**
 * Created by IVAN on 2019/2/28.
 */

public class LoadDialog implements DialogInterface.OnDismissListener, View.OnTouchListener {

    private AlertDialog mDialog;
    private Builder mBuilder;
    // View
    private View mDialogLayout;
    private ImageView mIv;
    private TextView mTv;
    // Logic
    private Animation mAnimation;
    private Rect mRect;

    private LoadDialog(Builder builder) {
        mBuilder = builder;
        mRect = new Rect();
    }

    public void show() {
        if (mDialog == null) {
            mDialog = new AlertDialog.Builder(mBuilder.context)
                    .setCancelable(mBuilder.cancelable)
                    .create();

            Window window = mDialog.getWindow();
            window.getDecorView().setBackgroundColor(Color.TRANSPARENT);
            WindowManager.LayoutParams params = window.getAttributes();
            params.dimAmount = 0.5f;// 背景
            window.setAttributes(params);

            mAnimation = getAnimation();

            LayoutInflater inflater = LayoutInflater.from(mBuilder.context);
            View contentView = inflater.inflate(R.layout.subject_all_dialog_load, null);
            initView(contentView);

            if (isFinish(mBuilder.context)) return;
            mDialog.show();
            mDialog.setContentView(contentView);
        } else {
            if (isFinish(mBuilder.context)) return;
            mDialog.show();
        }
        mIv.startAnimation(mAnimation);
    }

    private void initView(View contentView) {
        mDialogLayout = contentView.findViewById(R.id.dialogLayout);
        mIv = (ImageView) contentView.findViewById(R.id.iv);
        mTv = (TextView) contentView.findViewById(R.id.tv);

        if (!TextUtils.isEmpty(mBuilder.msg)) mTv.setText(mBuilder.msg);

        if (mBuilder.cancelable) contentView.setOnTouchListener(this);
    }

    private Animation getAnimation() {
        float pivotValue = 0.5f;
        float fromDegree = 5 * 360.0f;
        RotateAnimation animation = new RotateAnimation(
                fromDegree,
                0,
                Animation.RELATIVE_TO_SELF,
                pivotValue,
                Animation.RELATIVE_TO_SELF,
                pivotValue
        );
        animation.setFillAfter(true);
        animation.setInterpolator(new LinearInterpolator());
        animation.setDuration(3000);
        animation.setRepeatCount(Animation.INFINITE);
        animation.setRepeatMode(Animation.RESTART);
        return animation;
    }

    private boolean isFinish(Context context) {
        if (context instanceof Activity) if (((Activity) context).isFinishing()) return true;
        return false;
    }

    public void setMsg(CharSequence msg) {
        mBuilder.msg = msg;
        if (mTv != null) mTv.setText(msg);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        mIv.clearAnimation();
    }

    public void dismiss() {
        if (mDialog != null && mDialog.isShowing()) mDialog.dismiss();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mDialogLayout.getGlobalVisibleRect(mRect);
        if (!mRect.contains((int) event.getX(), (int) event.getY())) dismiss();
        return false;
    }

    public static class Builder {
        private Context context;
        private CharSequence msg;
        private boolean cancelable = true;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMsg(CharSequence msg) {
            this.msg = msg;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public LoadDialog build() {
            return new LoadDialog(this);
        }
    }
}
